package Game;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.io.File;
import java.io.Serializable;

import javax.imageio.ImageIO;
import javax.swing.*;

import GUIInterfaces.OregonPanel;
import GUIInterfaces.OregonWindow;

import java.util.*;

/**
 * 
 */

/**
 * @author William Kurkian
 *	Start screen
 * 	It expects the window above it to implement OregonWindow and it has oregon panel
 */
public class StartPanel extends JPanel implements ActionListener,OregonPanel, Serializable {
	
	GameDriver gd;
	Caravan caravan;
	Store store;
	OregonWindow parent;
	BufferedImage background;
	JButton start = new JButton();
	JButton load = new JButton();
	JButton quit = new JButton();
	String property;
	/*
	 * The constructor creates the components and initializes everything.
	 */
	public StartPanel(OregonWindow parent) {
		this.parent = parent;
		this.setLayout(null);
		
		configureButton(start,new Rectangle(438,361,402,54));
		configureButton(load,new Rectangle(457,465,374,54));
		configureButton(quit,new Rectangle(550,576,195,54));
		
		add(start);
		add(load);
		add(quit);
		String folder = System.getProperty("user.dir")+ File.separator + "images" + File.separator;
		try {
			background = ImageIO.read(new File(folder+"start.png"));
			
		} catch(Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
			System.out.println("The start page bckground did not load properly.");
		}
		
	}

	/**
	 * Method to configure buttons. It makes them invisible and sets their bounds to rectangle.
	 * @param button
	 * @param rectangle
	 */
	private void configureButton(JButton button, Rectangle rectangle) {
		button.setBounds(rectangle);
		button.setOpaque(false);
		button.setContentAreaFilled(false);
		button.setBorderPainted(false);
		button.addActionListener(this);
		
	}

	/**
	 * This method configures the passed in label.
	 * @param label
	 * @param rectangle
	 */
	private void configureLabel(JLabel label, Rectangle rectangle) {
		label.setBounds(rectangle);
		label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(),20));
	}
	
	public void paintComponent(Graphics g) {
		g.drawImage(background,0,0,null);
	}
	/**
	 * This method allows the parent to set the game driver after initialization
	 * so the panel can be initalized before the caravan is ready.
	 * This is from the OregonPanel.
	 * @param gd
	 */
	public void setGameDriver(GameDriver gd) {
		this.gd = gd;
		
		
	}
	
	@Override
	
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == start) {
			property = "new";
			parent.done(this);
		} else if (arg0.getSource() == load && (new GameSave()).saveExists()) {
			property = "load";
			parent.done(this);
		} else if (arg0.getSource() == quit) {
			System.exit(-1);
		}
	}

	public GameDriver getGameDriver() {
		return gd;
	}

	@Override
	public String getProperty() {
		// TODO Auto-generated method stub
		return property;
	}
}
